package com.dnake.libface;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.View;

import com.dnake.libface.core.Dmsg;
import com.dnake.libface.core.DnakeEventListener;
import com.dnake.libface.core.Dxml;
import com.dnake.libface.core.EventAnalyzer;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public class Face {
    public static final String TAG = "lib_dnake";

    /**
     * sdk初始化
     * @param context Context对象
     */
    public static void init(Context context) {
        context.startService(new Intent(context, DnakeService.class));
        Log.e("qpt","开启service");
    }


    /**
     * 删除单个人脸
     * @param id 人脸id
     */
    public static void deleteFace(int id) {
        ThreadPool.getInstance().execute(new Runnable() {
            @Override
            public void run() {
                Dxml p = new Dxml();
                Dmsg req = new Dmsg();
                p.setInt("/params/id", id);
                p.setInt("/params/uid", 0);
                req.to("/face/ex/del", p.toString());
            }
        });

    }

    /**
     * 清除所有人脸数据
     */
    public static void clearFace() {
        ThreadPool.getInstance().execute(new Runnable() {
            @Override
            public void run() {
                Dmsg req = new Dmsg();
                req.to("/face/local/clear", null);
            }
        });

    }


    /**
     * 开启人脸识别
     * @param similarity 相识度 72:低  75:中  78:高 （识别模式使用）
     * @param view 摄像头预览位置
     */
    public static void faceRecognition(int similarity, View view) {
        startFace(0, similarity, -1, view);
    }


    /**
     * 开启人脸注册
     * @param similarity 相识度 72:低  75:中  78:高 （识别模式使用）
     * @param registerId 人脸id 以此为唯一标识
     * @param view 摄像头预览位置
     */
    public static void faceRegister(int similarity, final int registerId, final View view) {
        startFace(1, similarity, registerId, view);
    }

    private static void startFace(int mode, int similarity, int registerId, View view) {
        ThreadPool.getInstance().execute(new Runnable() {
            @Override
            public void run() {
                Dmsg req = new Dmsg();
                Dxml p = new Dxml();

                if (mode == 0) {
                    p.setInt("/params/qr", 0);
                    p.setInt("/params/live", 0); //活体检测
                    p.setInt("/params/similarity", similarity);//相识度 72:低  75:中  78:高
                    req.to("/face/start", p.toString());
                } else {
                    p.setInt("/params/id", registerId);
                    req.to("/face/register", p.toString());
                }
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                p = new Dxml();
                if (view != null) {
                    int w = view.getRight() - view.getLeft();
                    int h = view.getBottom() - view.getTop();
                    if (w > 16 && h > 16) {
                        int[] xy = new int[2];
                        view.getLocationOnScreen(xy);
                        p.setInt("/params/x", xy[0]);
                        p.setInt("/params/y", xy[1]);
                        p.setInt("/params/w", w);
                        p.setInt("/params/h", h);
                        req.to("/face/osd", p.toString());
                    } else {
                        Log.e(Face.TAG, "faceStart is false,view is INVISIBLE");
                        return;
                    }
                } else {
                    int[] xy = new int[2];
                    p.setInt("/params/x", 0);
                    p.setInt("/params/y", 0);
                    p.setInt("/params/w", 0);
                    p.setInt("/params/h", 0);
                    req.to("/face/osd", p.toString());
                }

            }
        });
    }

    /**
     * 停止人脸识别，人脸注册，关闭人脸界面预览
     */
    public static void stopFace(){
           ThreadPool.getInstance().execute(new Runnable() {
               @Override
               public void run() {
                   Dmsg req = new Dmsg();
                   Dxml p = new Dxml();
                   p.setInt("/params/app", 1);
                   req.to("/face/stop", p.toString());
               }
           });
    }

    /**
     * 设置事件监听回调
     * @param listener 事件回调，包括人脸识别，注册，人体感应，卡号读取等
     */
    public static void setEventListener(@NotNull DnakeEventListener listener) {
        EventAnalyzer.setEventListener(listener);
    }
}
